<?php

/**
 * user actions.
 *
 * @package    maximous
 * @subpackage user
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends MaxControllerActions
{

    public function preExecute()
    {
        parent::preExecute();
        $this->maxUser->getMaxUserRole()->setDefaultSiteComponent(SiteComponent::User);
    }

    public function executeIndex(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canView());
        $this->formFilter = new UserFormFilter(array(), array('maxUser'    => $this->maxUser));
        $this->formFilter->bind($request->getParameter($this->formFilter->getName()));
        $query       = $this->formFilter->buildQuery($this->formFilter->getValues());
        $page        = $request->getParameter('page', 1);
        $this->pager = $this->createPager('User', $query, $page, sfConfig::get('app_table_list_row_numbers'));
    }

    public function executeNew(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canAdd());
        $this->form = new AddUserForm(null, array('i18n'            => $this->i18n, 'allowSelectRole' => true, 'noCaptcha'       => true, 'sfUser'          => $this->getUser(), 'maxUser'         => $this->maxUser));
    }

    public function executeCreate(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canAdd());
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->form = new AddUserForm(null, array('i18n'    => $this->i18n, 'sfUser'  => $this->getUser(), 'maxUser' => $this->maxUser));
        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
        if ($this->form->isValid())
        {
            $user = new User();
            $user = $this->form->save();
            $this->getUser()->setFlash('message_success', $this->i18n->__('add user') . ' ' . $user->getUsername() . ' ' . $this->i18n->__('successful'));
            $this->redirect('user/index');
        }
        else
        {
            $this->getUser()->setFlash('message_failed', $this->i18n->__('add user') . ' ' . $this->i18n->__('failed'));
        }

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canEdit());
        $this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
        $this->form = new AddUserForm($user, array('i18n'    => $this->i18n, 'sfUser'  => $this->getUser(), 'maxUser' => $this->maxUser));
    }

    public function executeUpdate(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canEdit());
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
        $this->form = new AddUserForm($user, array('i18n'            => $this->i18n, 'maxUser'         => $this->maxUser, 'allowSelectRole' => true, 'noCaptcha'       => true, 'sfUser'          => $this->getUser()));
        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

        if ($this->form->isValid())
        {
            $user = $this->form->save();
            $this->getUser()->setFlash('message_success', $this->i18n->__('edit user') . ' ' . $user->getUsername() . ' ' . $this->i18n->__('successful'));
        }
        else
        {
//            pre($this->form->getGlobalErrors());
            $this->getUser()->setFlash('message_failed', $this->i18n->__('edit user') . ' ' . $user->getUsername() . ' ' . $this->i18n->__('failed'));
        }
        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canDelete());
        $this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id'))), sprintf('Object user does not exist (%s).', $request->getParameter('id')));
        $this->getUser()->setFlash('message_success', $this->i18n->__('delete user') . ' ' . $user->getUsername() . ' ' . $this->i18n->__('successful'));
        $user->delete();
        $this->getUser()->setFlash('delete_user_message', __('delete user success'));
        $this->redirect('user/index');
    }

    public function executeEditMyData(sfWebRequest $request)
    {
        $user = UserTable::getInstance()->findOneById($this->maxUser->getUserDataByIndex('id'));
        $form = new EditMyDataForm($user, array('i18n'    => $this->i18n, 'sfUser'  => $this->getUser(), 'maxUser' => $this->maxUser));
        if ($request->isMethod(sfWebRequest::POST))
        {
            $form->bind($request->getParameter($form->getName()), $request->getFiles());

            if ($form->isValid())
            {
                $user = $form->save();
                $this->getUser()->setFlash('message_success', $this->i18n->__('edit user') . ' ' . $user->getUsername() . ' ' . $this->i18n->__('successful'));
                $this->redirect($this->generateUrl('EditMyData'));
            }
            else
            {
                $this->getUser()->setFlash('message_failed', $this->i18n->__('edit user') . ' ' . $user->getUsername() . ' ' . $this->i18n->__('failed'));
            }
        }
        $this->form = $form;
    }

}
